package com.example.lss.news;

import android.content.Intent;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.lss.news.bean.User;
import com.example.lss.news.bean.UserBean;
import com.example.lss.news.dao.NewsDao;
import com.example.lss.news.utils.JsonUtils;
import com.example.lss.news.utils.app.BaseActivity;
import com.example.lss.news.utils.db.DBHelper;
import com.example.lss.news.utils.http.HttpUtils;
import com.example.lss.news.utils.http.JsonCallback;
import com.example.lss.news.utils.pic.CircleImageView;
import com.example.lss.news.utils.pic.GlideApp;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, TextWatcher {
    private Button login, register;
    private CircleImageView imageView;
    private EditText acc, pwd;
    private NewsDao dao = new NewsDao();
    private TextView tiaoguo;

    @Override
    protected int getLayoutId() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
//        getWindow().getDecorView().setBackgroundResource(R.drawable.qidongye);

        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        acc = findViewById(R.id.acc);
        pwd = findViewById(R.id.pwd);
        tiaoguo = findViewById(R.id.tiaoguo);
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        imageView = findViewById(R.id.head);
        acc.addTextChangedListener(this);


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        register.setOnClickListener(this);
        login.setOnClickListener(this);
        tiaoguo.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == register) {
            jumpToActivity(RegisterActivity.class);
        } else if (view == login) {
            final String url = "https://www.apiopen.top/login?key=00d91e8e0cca2b76f515926a36db68f5&phone=" + acc.getText().toString() + "&passwd=" + pwd.getText().toString();

            HttpUtils.get(url, new JsonCallback() {
                @Override
                public void onStart(Object tag) {

                }

                @Override
                public void onSuccess(String json) {
                    UserBean bean = JsonUtils.toBean(json, UserBean.class);
                    if ("200".equals(bean.getCode())) {
                        Intent it = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(it);
                        finish();
                    } else {
                        showToast("密码错误！！！");
                    }
                }

                @Override
                public void onError(Throwable exception) {

                }

                @Override
                public void onFinish() {

                }
            });
        } else if (view == tiaoguo) {
            Intent it = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(it);
            finish();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    //判断是否存在于数据库中的账号
    @Override
    public void afterTextChanged(Editable editable) {
        for (int i = 0; i < dao.show().size(); i++) {
            if (acc.getText().toString().equals(dao.show().get(i).getPhone())) {
                String url = dao.show().get(i).getImg().substring(25);
                saveSP("head", dao.show().get(i).getPhone());
                GlideApp.with(this).load(url).into(imageView);
            }
        }

    }
}
